package com.seamtop.cuber.server.job;

import com.seamtop.cuber.common.entriy.CuberReducerJob;
import com.seamtop.cuber.common.entriy.CuberReducer;
import com.seamtop.cuber.common.entriy.Duration;
import com.seamtop.cuber.common.util.DateUtil;
import com.seamtop.cuber.server.service.CuberJobService;
import com.seamtop.cuber.server.service.ToolSpring;
import com.seamtop.cuber.server.service.impl.CuberJobServiceImpl;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by zongjunfeng on 2015/9/28.
 */
@Service
public class ReducerJobFactory implements Job {

    private static final Logger LOG = LoggerFactory.getLogger(ReducerJobFactory.class);


    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        CuberReducer reducer = (CuberReducer)context.getMergedJobDataMap().get("reducer");
        System.out.println("reducer:"+reducer.getStatPeriod());

        Duration duration = JobKits.matchDuration(reducer.getStatPeriod());
        CuberReducerJob cuberReducerJob = new CuberReducerJob();
        cuberReducerJob.setReducer(reducer);
        cuberReducerJob.setDuration(duration);
        LOG.info("Reducer[" + reducer.getReducerId() + "] execute start!--------------------------------Time:"+ DateUtil.getFullDateStr(new Date()));
        try{
            CuberJobService cuberJobService = (CuberJobServiceImpl)ToolSpring.getApplicationContext().getBean("cuberJobService");
            cuberJobService.execute(cuberReducerJob);
        }catch (Exception e){
            LOG.error("Reducer["+reducer.getReducerId()+"] execute error!",e);
        }
        LOG.info("Reducer["+reducer.getReducerId()+"] execute success!");
    }

}
